package com.hzx.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class RandomString {
    static StringBuilder pool = new StringBuilder("");

    static {
        //0123456789ABCDEF..Zabcd..z
        for (short i = '0'; i <= '9'; i++) {
            pool.append((char) i);
        }
        for (short i = 'A'; i <= 'Z'; i++) {
            pool.append((char) i);
        }
        for (short i = 'a'; i <= 'z'; i++) {
            pool.append((char) i);
        }
    }

    public static List<Object> getString(int aId, int size) {
        Set<String> stringSet = new HashSet<>();
        int len = 5;
        List<Object> stringList = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            String temp = randomString(len);
            while (stringSet.contains(temp)) {
                len++;
                temp = randomString(len);
            }
            stringSet.add(temp);
            stringList.add(aId + "#" + temp);
        }
        return stringList;
    }

    public static String randomString(int a) {
        char[] cs = new char[a];
        for (int h = 0; h < cs.length; h++) {
            int index = (int) (Math.random() * pool.length());//产生一个pool范围内的随机数
            cs[h] = pool.charAt(index);//返回指定索引处的字符
        }
        return new String(cs);
    }
}